1
|
|
|
import merge from "../help/merge.js"; |
2
|
|
|
import {calculateEncodingAttributes, getTotalWidthOfEncodings, getMaximumHeightOfEncodings} from "./shared.js"; |
3
|
|
|
|
4
|
|
|
var svgns = "http://www.w3.org/2000/svg"; |
5
|
|
|
|
6
|
|
|
class SVGRenderer{ |
7
|
|
|
constructor(svg, encodings, options){ |
8
|
|
|
this.svg = svg; |
9
|
|
|
this.encodings = encodings; |
10
|
|
|
this.options = options; |
11
|
|
|
this.document = options.xmlDocument || document; |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
render(){ |
15
|
|
|
var currentX = this.options.marginLeft; |
16
|
|
|
|
17
|
|
|
this.prepareSVG(); |
18
|
|
|
for(let i = 0; i < this.encodings.length; i++){ |
19
|
|
|
var encoding = this.encodings[i]; |
20
|
|
|
var encodingOptions = merge(this.options, encoding.options); |
21
|
|
|
|
22
|
|
|
var group = this.createGroup(currentX, encodingOptions.marginTop, this.svg); |
23
|
|
|
|
24
|
|
|
this.setGroupOptions(group, encodingOptions); |
25
|
|
|
|
26
|
|
|
this.drawSvgBarcode(group, encodingOptions, encoding); |
27
|
|
|
this.drawSVGText(group, encodingOptions, encoding); |
28
|
|
|
|
29
|
|
|
currentX += encoding.width; |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
prepareSVG(){ |
34
|
|
|
// Clear the SVG |
35
|
|
|
while (this.svg.firstChild) { |
36
|
|
|
this.svg.removeChild(this.svg.firstChild); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
calculateEncodingAttributes(this.encodings, this.options); |
40
|
|
|
var totalWidth = getTotalWidthOfEncodings(this.encodings); |
41
|
|
|
var maxHeight = getMaximumHeightOfEncodings(this.encodings); |
42
|
|
|
|
43
|
|
|
var width = totalWidth + this.options.marginLeft + this.options.marginRight; |
44
|
|
|
this.setSvgAttributes(width, maxHeight); |
45
|
|
|
|
46
|
|
|
if(this.options.background){ |
47
|
|
|
this.drawRect(0, 0, width, maxHeight, this.svg).setAttribute( |
48
|
|
|
"style", "fill:" + this.options.background + ";" |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
drawSvgBarcode(parent, options, encoding){ |
54
|
|
|
var binary = encoding.data; |
55
|
|
|
|
56
|
|
|
// Creates the barcode out of the encoded binary |
57
|
|
|
var yFrom; |
58
|
|
|
if(options.textPosition == "top"){ |
59
|
|
|
yFrom = options.fontSize + options.textMargin; |
60
|
|
|
} |
61
|
|
|
else{ |
62
|
|
|
yFrom = 0; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
var barWidth = 0; |
66
|
|
|
var x = 0; |
67
|
|
|
for(var b = 0; b < binary.length; b++){ |
68
|
|
|
x = b * options.width + encoding.barcodePadding; |
69
|
|
|
|
70
|
|
|
if(binary[b] === "1"){ |
71
|
|
|
barWidth++; |
72
|
|
|
} |
73
|
|
|
else if(barWidth > 0){ |
74
|
|
|
this.drawRect(x - options.width * barWidth, yFrom, options.width * barWidth, options.height, parent); |
75
|
|
|
barWidth = 0; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// Last draw is needed since the barcode ends with 1 |
80
|
|
|
if(barWidth > 0){ |
81
|
|
|
this.drawRect(x - options.width * (barWidth - 1), yFrom, options.width * barWidth, options.height, parent); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
drawSVGText(parent, options, encoding){ |
86
|
|
|
var textElem = this.document.createElementNS(svgns, 'text'); |
87
|
|
|
|
88
|
|
|
// Draw the text if displayValue is set |
89
|
|
|
if(options.displayValue){ |
90
|
|
|
var x, y; |
91
|
|
|
|
92
|
|
|
textElem.setAttribute("style", |
93
|
|
|
"font:" + options.fontOptions + " " + options.fontSize + "px " + options.font |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
if(options.textPosition == "top"){ |
97
|
|
|
y = options.fontSize - options.textMargin; |
98
|
|
|
} |
99
|
|
|
else{ |
100
|
|
|
y = options.height + options.textMargin + options.fontSize; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// Draw the text in the correct X depending on the textAlign option |
104
|
|
|
if(options.textAlign == "left" || encoding.barcodePadding > 0){ |
105
|
|
|
x = 0; |
106
|
|
|
textElem.setAttribute("text-anchor", "start"); |
107
|
|
|
} |
108
|
|
|
else if(options.textAlign == "right"){ |
109
|
|
|
x = encoding.width - 1; |
110
|
|
|
textElem.setAttribute("text-anchor", "end"); |
111
|
|
|
} |
112
|
|
|
// In all other cases, center the text |
113
|
|
|
else{ |
114
|
|
|
x = encoding.width / 2; |
115
|
|
|
textElem.setAttribute("text-anchor", "middle"); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
textElem.setAttribute("x", x); |
119
|
|
|
textElem.setAttribute("y", y); |
120
|
|
|
|
121
|
|
|
textElem.appendChild(this.document.createTextNode(encoding.text)); |
122
|
|
|
|
123
|
|
|
parent.appendChild(textElem); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
setSvgAttributes(width, height){ |
129
|
|
|
var svg = this.svg; |
130
|
|
|
svg.setAttribute("width", width + "px"); |
131
|
|
|
svg.setAttribute("height", height + "px"); |
132
|
|
|
svg.setAttribute("x", "0px"); |
133
|
|
|
svg.setAttribute("y", "0px"); |
134
|
|
|
svg.setAttribute("viewBox", "0 0 " + width + " " + height); |
135
|
|
|
|
136
|
|
|
svg.setAttribute("xmlns", svgns); |
137
|
|
|
svg.setAttribute("version", "1.1"); |
138
|
|
|
|
139
|
|
|
svg.setAttribute("style", "transform: translate(0,0)"); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
createGroup(x, y, parent){ |
143
|
|
|
var group = this.document.createElementNS(svgns, 'g'); |
144
|
|
|
group.setAttribute("transform", "translate(" + x + ", " + y + ")"); |
145
|
|
|
|
146
|
|
|
parent.appendChild(group); |
147
|
|
|
|
148
|
|
|
return group; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
setGroupOptions(group, options){ |
152
|
|
|
group.setAttribute("style", |
153
|
|
|
"fill:" + options.lineColor + ";" |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
drawRect(x, y, width, height, parent){ |
158
|
|
|
var rect = this.document.createElementNS(svgns, 'rect'); |
159
|
|
|
|
160
|
|
|
rect.setAttribute("x", x); |
161
|
|
|
rect.setAttribute("y", y); |
162
|
|
|
rect.setAttribute("width", width); |
163
|
|
|
rect.setAttribute("height", height); |
164
|
|
|
|
165
|
|
|
parent.appendChild(rect); |
166
|
|
|
|
167
|
|
|
return rect; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
export default SVGRenderer; |
172
|
|
|
|